<template>
    <el-container class="home_container">
      <!-- 头部区域 -->
      <el-header>
        <div class="header_title">
          <img src="../assets/logo.png" alt="">
          <p>电商后台管理系统</p>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <!-- 左侧侧边栏区域 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 折叠按钮 -->
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409Eff"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath">
            <!-- 一级菜单 -->
            <!-- index 只接收字符串，所以在后面拼接一个空字符串 -->
            <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/' + secitem.path" v-for="secitem in item.children" :key="secitem.id" @click="savaNavState('/' + secitem.path)">
                <i class="el-icon-menu"></i>
                <span>{{ secitem.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </template>
  
  <script>
  export default {
    data() {
      return {
        // 左侧菜单列表
        menulist: [],
        // icon 图标
        iconObj: {
          125: 'el-icon-s-custom',
          103: 'el-icon-s-tools',
          101: 'el-icon-s-goods',
          102: 'el-icon-s-order',
          145: 'el-icon-s-marketing'
        },
        isCollapse: false,
        // 被激活的链接地址
        activePath: ''
      }
    },
    methods: {
      logout() {
        window.sessionStorage.removeItem('token')
        this.$router.go(-1)
      },
      // 获取所有菜单数据
      async getMenuList() {
        const { data: res } = await this.$http.get('/api/getmenu')
        if (res.meta.status !== 200) return this.$msg.error('获取菜单列表失败')
        this.menulist = res.data
      },
      // 点击按钮折叠与展开菜单栏
      toggleCollapse() {
        this.isCollapse = !this.isCollapse
      },
      savaNavState(activePath) {
        window.sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
      }
    },
    created() {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    }
  }
  </script>
  
  <style lang="less" scoped>
    .home_container {
      height: 100%;
      
    }
  
    .el-header {
      display: flex;
      justify-content: space-between;
      background-color: #373d41;
  
      .el-button {
        align-self: center;
      }
    }
  
    .el-aside {
      background-color: #333744;
      .el-menu {
        border-right: none
      }
    }
  
    .el-main {
      background-color: #eaedf1;
    }
  
    .header_title {
      height: 60px;
      width: 200px;
      display: flex;
      justify-content: space-between;
  
      img {
        height: 60px;
      }
      p {
        color: #fff;
      }
    }
  
    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
  </style>